'use strict';
const db = uniCloud.database();
const floorsCollection = db.collection('floors');

exports.main = async (event, context) => {
	const { floor_number, description, token } = event;
    
    // 只检查token是否存在
    if (!token) {
        return {
            code: 401,
            message: '未登录'
        };
    }
	
	// 参数校验
	if (!floor_number || typeof floor_number !== 'number') {
		return {
			code: 1,
			message: '楼层号参数无效，应为数字'
		};
	}
	
	// 检查楼层是否已存在
	const checkResult = await floorsCollection.where({
		floor_number: floor_number
	}).count();
	
	if (checkResult.total > 0) {
		return {
			code: 2,
			message: '该楼层号已存在'
		};
	}
	
	// 添加楼层
	const addResult = await floorsCollection.add({
		floor_number: floor_number,
		description: description || '',
		create_time: new Date(),
		update_time: new Date()
	});
	
	return {
		code: 0,
		message: '楼层添加成功',
		data: {
			id: addResult.id
		}
	};
}; 